package net.lab1024.sa.admin.util;

import org.flowable.engine.*;
import org.flowable.engine.impl.test.PluggableFlowableTestCase;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import javax.annotation.Resource;
import java.util.Map;

import static org.hibernate.validator.internal.util.Contracts.assertNotNull;

/**
 * 流程工具类
 *
 * @author konbai
 * @since 2022/12/11 03:35
 */
public class ProcessTestUtils extends PluggableFlowableTestCase {


    @Resource
    private RepositoryService repositoryService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private IdentityService identityService;

    @Resource
    private TaskService taskService;

    @Resource
    private HistoryService historyService;



    public void testProcessSimulation(String processDefinitionKey, Map<String, Object> variables) {
        // 启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);

        // 模拟执行流程中的一个任务
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        assertNotNull(task);


        // 模拟任务完成
        taskService.complete(task.getId());

        // 验证流程是否结束
        assertProcessEnded(processInstance.getId());

        //回滚流程实例
        runtimeService.deleteProcessInstance(processInstance.getId(), "回滚测试");


    }

}
